package com.fau.template.util;

import java.util.List;
import java.util.Map;

public class ParamUtil {
	
	public static Boolean isSearch(List<Map<String, Object>> listMap) {
		String s = getString(listMap, "sSearch");
		if(null == s || "".equals(s)) return false;
		return true;
	}
	
	public static Boolean isSortVal(List<Map<String, Object>> listMap) {
		Integer i = getInteger(listMap, "iSortCol_0");
		if(null == i || 0 == i) return false;
		if(null == getString(listMap, "sSortDir_0")) return false;
		if(null == getString(listMap, "mDataProp_0")) return false;
		return true;
	}
	
	public static String[] getSortCol(List<Map<String, Object>> listMap, String key) {
		String[] str = new String[2];
		Integer i = getInteger(listMap, key);
		str[0] = getString(listMap, "mDataProp_" + i);
		str[1] = getString(listMap, "sSortDir_0");
		return str;
	}
	
	public static String getString(List<Map<String, Object>> listMap, String key) {
		return getString(listMap, key, null);
	}
	
	public static String getString(List<Map<String, Object>> listMap, String key, String defaultValue) {
		if(null == getArray(listMap, key)) return defaultValue;
		return getArray(listMap, key);
	}
	
	public static Integer getInteger(List<Map<String, Object>> listMap, String key) {
		return getInteger(listMap, key, null);
	}
	
	public static Integer getInteger(List<Map<String, Object>> listMap, String key, Integer defaultValue) {
		if(null == getString(listMap, key)) return defaultValue;
		Integer i = Integer.parseInt(getString(listMap, key));
		return i;
	}
	
	private static String getArray(List<Map<String, Object>> listMap, String key) {
		return getArray(listMap, key, null);
	}
	
	private static String getArray(List<Map<String, Object>> listMap, String key, String defaultValue) {
		String val = null;
		if(null == listMap) return defaultValue;
		
		for(Map<String, Object> map : listMap) {
			if(map.get("name").equals(key)) {
				if(null == map.get("value")) {	
					val = defaultValue;
					break;
				} else {
					val = map.get("value").toString();
					break;
				}
			}
		}
		
		return val;
	}

}
